package com.qhy.springcloud.service.impl;

import com.qhy.springcloud.dao.AccountMapper;
import com.qhy.springcloud.domain.CommonResult;
import com.qhy.springcloud.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

/**
 * @author qiuhongyu
 * @date 2020/12/8 11:12
 */
@Service
public class AccountserviceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    public CommonResult decrease(Long userId, BigDecimal money) {
        //模拟超时异常，全局事务回滚
        try {
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int result = accountMapper.decrease(userId, money);
        if (result != 1) {
            return new CommonResult(409, "账户扣减金额失败！");
        }
        return new CommonResult(200, "账户扣减金额成功！");
    }
}
